/*
 * 创建日期 2009-11-11
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.cpp.core.item.tree;

import java.util.List;

import  org.fantasy.cpp.core.item.AbstractItem;
import  org.fantasy.common.db.bean.Row;

/**
 * 树
 * @author 王文成
 * @version 1.0
 * @since 2011-4-19
 */
public abstract class AbstractTreeItem extends AbstractItem {

	@Override
	@SuppressWarnings("unchecked")
	public String getHtml() throws Exception {
		StringBuffer html = new StringBuffer(1024);
		String code = item.getItemCode();
		String name = item.getItemName();
		List<Row> options = (List<Row>) item.getSource();
		html.append("  <select " + getStyleHtml() + " id=\"" + code + "\" name=\"" + code + "\" desc='" + name + "' " + getClassHtml() + "> \n");
		html.append("      " + getOptionsHtml(options) + " \n");
		html.append("  </select> \n");
		return html.toString();
	}

	/**
	 * 生成Option
	 * 
	 * @return
	 */
	protected String getOptionsHtml(List<Row> options) throws Exception {
		StringBuffer html = new StringBuffer(1024);
		String defaultValue = item.getDefValue();
		for (Row row : options) {
			String value = row.getString("value");
			String text = row.getString("text");
			if (value.equals(defaultValue))
				html.append("      <option value=\"" + value + "\" selected=\"true\">" + text + "</option> \n");
			else
				html.append("      <option value=\"" + value + "\">" + text + "</option> \n");
		}
		return html.toString();
	}
}
